package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entitry.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@Transactional
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    EmployeeService employeeService;


    //登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {


        //将前端获取的密码进行加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //条件查询，判断用户名密码是否正确
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername()).eq(Employee::getPassword, password);
        Employee one = employeeService.getOne(lqw);

        //查不到，代表用户名或者密码错误
        if (one == null || one.getStatus() == 0) {
            return R.error("登陆失败！");
        } else {

            //查到了，登陆成功
            request.getSession().setAttribute("employee", one.getId());
            return R.success(one);
        }

    }


    //登出

    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {

        //退出登录，清空session
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }


    //添加员工
    @PostMapping
    public R addEmployee(HttpServletRequest request, @RequestBody Employee employee) {

        //设置默认密码，并加密
        String password = "123456";
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(pwd);

        //设置创建、修改者
//        employee.setCreateUser((Long)(request.getSession().getAttribute("employee")));
//        employee.setUpdateUser((Long)(request.getSession().getAttribute("employee")));


        //设置创建、修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //调用添加函数
        boolean save = employeeService.save(employee);
        if (save) {
            return R.success(1);
        }
        return R.error("添加失败！");
    }


    //分页模糊查询
    @GetMapping("/page")
    public R<IPage> selectPage(int page, int pageSize, String name) {

        //分页模糊查询，根据前端发送的数据，使用条件拼接，进行查询
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null && !(name.equals("")), Employee::getUsername, name);
        IPage getPage = new Page(page, pageSize);
        employeeService.page(getPage, lqw);
        return R.success(getPage);
    }


    //修改员工信息
    @PutMapping
    public R updateStatus(HttpServletRequest request, @RequestBody Employee employee) {


//        employee.setUpdateUser((Long)(request.getSession().getAttribute("employee")));
//        employee.setUpdateTime(LocalDateTime.now());
        //修改员工状态
        boolean updateById = employeeService.updateById(employee);
        if (updateById) {
            return R.success("修改成功！");
        }
        return R.error("修改失败！");
    }


    //获取员工信息，用于修改回显
    @GetMapping("/{id}")
    public R<Employee> getEmployee(@PathVariable Long id) {
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("操作失败！");
    }

}
